const studentsModel = require('../model/students');

let list = async (ctx, next) => {
  let { page = 1, size = 20 } = ctx.request.body
    let offset = (page - 1) * size;
    let list = await studentsModel.findAndCountAll({
        //offet去掉前多少个数据
        offset,
        //limit每页数据数量
        limit: size
    }).then(res => {
        let result = {};
        result.rows = res.rows;
        result.total = res.count;
        return result;
    });
    return ctx.send({
      data: list,
      resultCode: 1,
      state: 200,
      type: 'success'
    })
};
let detail =   async (ctx, next) => {
  let { id } = ctx.request.body
  if(!id){
    return ctx.send({
      data: {},
      errorDesc: '参数错误',
      resultCode: 0,
      state: 200,
      type: 'error' 
    })
  }
  let res = await studentsModel.findAll({
    where: {
      id: id
    }
  });
  if(res[0]){
    return ctx.send({
      data: res[0] || '',
      resultCode: 1,
      state: 200,
      type: 'success'
    })
  }else{
    return ctx.send({
      data: '',
      errorDesc: '数据不存在',
      resultCode: 0,
      state: 200,
      type: 'error' 
    })
  }
};
// INSERT INTO `students` (`id`, `name`, `gender`, `major`, `age`, `no`, `grade`, `createdAt`, `updatedAt`)
// VALUES
// 	(1, '陈校', 0, '计算机', 18, '888', '大一', '2021-07-20 00:00:00', '2021-07-20 00:00:00');

let add = async (ctx, next) => {
  let { name,major,gender,age,no,grade  } = ctx.request.body
  if(!name || !major|| !gender || !age || !no || !grade){
    return ctx.send({
      data: {},
      errorDesc: '参数错误',
      resultCode: 0,
      state: 200,
      type: 'error' 
    })
  }
  await studentsModel.create({
    name,major,gender,age,no,grade 
  });
  return ctx.send({
    data: {},
    resultCode: 1,
    state: 200,
    type: 'success' // 自定义响应体
  })
};
let updated = async (ctx, next) => {
  let { id, name,major,gender,age,no,grade  } = ctx.request.body
  if(!id){
    return ctx.send({
      data: {},
      errorDesc: '参数错误',
      resultCode: 0,
      state: 200,
      type: 'error' 
    })
  }
  let res = await studentsModel.findAll({
    where: {
      id: id
    }
  });
  let data = res[0]
  if(!data){
    return ctx.send({
      data: '',
      errorDesc: '数据不存在',
      resultCode: 0,
      state: 200,
      type: 'error' 
    })
  }
  let d = {
    name: name || data.name,
    major: major || data.major,
    gender: gender || data.gender,
    age: age || data.age,
    no: no || data.no,
    grade : grade || data.grade,
  }
  await studentsModel.update(d, {
    where: {
      id: id
    }
  });
  return ctx.send({
    data: {},
    resultCode: 1,
    state: 200,
    type: 'success' // 自定义响应体
  })
};
let del = async (ctx, next) => {
  let { id } = ctx.request.body
  if(!id){
    return ctx.send({
      data: {},
      errorDesc: '参数错误',
      resultCode: 0,
      state: 200,
      type: 'error' 
    })
  }
  await studentsModel.destroy({
    where: {
      id: id
    }
  });
  return ctx.send({
    data: {},
    resultCode: 1,
    state: 200,
    type: 'success' // 自定义响应体
  })
};

// module.exports = {
//   'POST /students/list': list,
//   'POST /students/detail': detail,
//   'POST /students/add': add,
//   'POST /students/updated': updated,
//   'POST /students/del': del,
// };

module.exports = {
  'POST TOKEN /students/list': list,
  'POST TOKEN /students/detail': detail,
  'POST TOKEN /students/add': add,
  'POST TOKEN /students/updated': updated,
  'POST TOKEN /students/del': del,
};